package org.extreme.util;

import java.util.LinkedList;


/**
 * Thread local stack utility class.
 */
public class ThreadLocalStack
{
	private final ThreadLocal threadStack;
	
	public ThreadLocalStack()
	{
		threadStack = new ThreadLocal();
	}
	
	public void push(Object o)
	{
		LinkedList stack = (LinkedList) threadStack.get();
		if (stack == null)
		{
			stack = new LinkedList();
			threadStack.set(stack);
		}
		stack.addFirst(o);
	}
	
	public Object top()
	{
		Object o = null;
		LinkedList stack = (LinkedList) threadStack.get();
		if (stack != null && !stack.isEmpty())
		{
			o = stack.getFirst();
		}
		return o;
	}
	
	public Object pop()
	{
		Object o = null;
		LinkedList stack = (LinkedList) threadStack.get();
		if (stack != null)
		{
			o = stack.removeFirst();
		}
		return o;
	}
	
	public boolean empty()
	{
		LinkedList stack = (LinkedList) threadStack.get();
		return stack == null || stack.isEmpty();
	}
}
